/**
 * 
 * Copyright 2009 Marco Speranza <marco.speranza79@gmail.com>
 *    
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *   
 * http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.commons.database.util.jquerycreator;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// TODO: Auto-generated Javadoc
/**
 * Classe che implemenata un Default Datasource manager. Crea e gestisce la
 * connessione al Datasource.
 * 
 * @author Marco Speranza
 */
public class DefaultDatasourceManager implements IDatasourceManager {

    /** The username. */
    private String username;

    /** The password. */
    private String password;

    /** The url. */
    private String url;

    /**
     * Crea una istanza dlela classe.
     * 
     * @param username
     *            l'user name per la connessione al db
     * @param password
     *            la password name per la connessione al db
     * @param url
     *            l'url per la connessione al db
     * @param driverClassName
     *            il class name per del <code>java.sql.Driver</code>
     * 
     * @throws InstantiationException
     *             the instantiation exception
     * @throws IllegalAccessException
     *             the illegal access exception
     * @throws ClassNotFoundException
     *             the class not found exception
     * @throws SQLException
     *             the SQL exception
     */
    public DefaultDatasourceManager(String username, String password,
	    String url, String driverClassName) throws InstantiationException,
	    IllegalAccessException, ClassNotFoundException, SQLException {
	this(username, password, url, driverClassName, null);
    }

    /**
     * Crea una istanza dlela classe.
     * 
     * @param username
     *            l'user name per la connessione al db
     * @param password
     *            la password name per la connessione al db
     * @param url
     *            l'url per la connessione al db
     * @param driverClassName
     *            il class name per del <code>java.sql.Driver</code>
     * @param writer
     *            il print writer per i log.
     * 
     * @throws InstantiationException
     *             the instantiation exception
     * @throws IllegalAccessException
     *             the illegal access exception
     * @throws ClassNotFoundException
     *             the class not found exception
     * @throws SQLException
     *             the SQL exception
     */
    public DefaultDatasourceManager(String username, String password,
	    String url, String driverClassName, PrintWriter writer)
	    throws InstantiationException, IllegalAccessException,
	    ClassNotFoundException, SQLException {

	this.username = username;
	this.password = password;
	this.url = url;
	if (driverClassName != null) {
	    Class.forName(driverClassName).newInstance();
	}
	if (writer != null) {
	    setLogWriter(writer);
	}
    }

    /**
     * Sets the log writer.
     * 
     * @param writer
     *            the new log writer
     */
    public void setLogWriter(PrintWriter writer) {
	DriverManager.setLogWriter(writer);
    }

    /**
     * Gets the connection.
     * 
     * @return the connection
     * 
     * @throws SQLException
     *             the SQL exception
     */
    public Connection getConnection() throws SQLException {
	return DriverManager.getConnection(url, username, password);
    }

    /**
     * Release connection.
     * 
     * @param connection
     *            the connection
     * 
     * @throws SQLException
     *             the SQL exception
     */
    public void releaseConnection(Connection connection) throws SQLException {
	if (connection != null) {
	    connection.close();
	}

    }

}
